﻿using Microsoft.Kinect;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace BT.Kinect 
{
    class Button : DrawableGameComponent
    {
        SpriteBatch spriteBatch;

        KinectInput chooser;

        SpriteFont f;
        Vector2 position;

        bool status;
        Texture2D button;
        Vector2 buttonPosition;
        
        public bool Paused
        {
            get { return !chooser.SensorFound || !chooser.PlayerFound; }
        }

        public Button(Game game,Vector2 buttonPosition) : base(game)
        {
            this.buttonPosition = buttonPosition;
            game.Components.Add(this);
        }

        public override void Initialize()
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            chooser = (KinectInput)Game.Services.GetService(typeof(KinectInput));

            base.Initialize();
        }

        protected override void LoadContent()
        {
            //background = Game.Content.Load<Texture2D>("kinectbackground");
           // font = Game.Content.Load<SpriteFont>("Segoe16");
            f = Game.Content.Load<SpriteFont>("Segoe16");
            button = Game.Content.Load<Texture2D>("Button");
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            
        }

        public override void Update(GameTime gameTime)
        {
            if (Paused)
                return;

            if (chooser.Skeleton != null)
                position = ScaleTo(chooser.Skeleton.Joints[JointType.HandRight], GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            
            base.Update(gameTime);
        }

        public static Vector2 ScaleTo(Joint joint, int width, int height)
        {
            float x = Scale(width, 0.30f, joint.Position.X);
            float y = Scale(height, 0.30f, -joint.Position.Y);
            return new Vector2(x, y);
        }

        private static float Scale(int maxPixel, float maxSkeleton, float position)
        {
            float value = ((maxPixel / maxSkeleton / 2 * position) + (maxPixel / 2));

            if (value > maxPixel)
                return maxPixel;

            if (value < 0)
                return 0;

            return value;
        }

        //draw hand
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.DrawString(f, "aaa", position, Color.Red);
           // spriteBatch.End();

            Rectangle rectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y,
               (int)(1 * button.Width), (int)(1 * button.Height));
         //   spriteBatch.Begin();
            spriteBatch.Draw(button, rectangle, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
        //draw button
        public void Draw(float scaleW, float scaleH)
        {
            /*
            Rectangle rectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y,
                (int)(scaleW * button.Width), (int)(scaleH * button.Height));
            spriteBatch.Begin();
            spriteBatch.Draw(button, rectangle, Color.White);
            spriteBatch.End();*/
        }

    }
}
